<template>
  <div class="login-container">
    <el-card class="box-card" shadow="always">
      <el-col span="11">
        <div style="display: table-cell; vertical-align: middle; text-align: center">
          <span class="left-title">扫码登录</span>
          <span class="left-title" style="color: #bfbfbf; font-size: 14px">使用微信“扫一扫”登录</span>
          <el-image src="https://pica.zhimg.com/36940066ff196785773634369b4290c8_r.jpg?source=1940ef5c" class="QR-img"></el-image>
          <span class="left-title"></span>
        </div>
      </el-col>

      <el-col span="1">
        <div class="line"></div>
      </el-col>

      <el-col span="11" class="register-box">
        <span>用户注册</span>
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules">
          <el-form-item prop="phone">
            <el-input v-model="loginForm.phone" placeholder="请输入手机号" prefix-icon="el-icon-user" style="margin-top: 30px;"></el-input>
          </el-form-item>
          <el-form-item prop="captcha">
            <div style="display: flex; align-items: center">
              <el-input v-model="loginForm.captcha" placeholder="6 位短信验证码" prefix-icon="el-icon-lock" style="width: 100%; margin-right: 10px"></el-input>
              <GetCaptchaButton style="width: auto" :phone="loginForm.phone"></GetCaptchaButton>
            </div>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" placeholder="请输入密码" prefix-icon="el-icon-lock" show-password></el-input>
          </el-form-item>
          <el-form-item prop="checkPassword">
            <el-input v-model="loginForm.checkPassword" placeholder="请再次输入密码" prefix-icon="el-icon-lock" show-password></el-input>
          </el-form-item>
        </el-form>
        <el-button type="primary" @click="register" class="login-button">注册</el-button>
        <el-row>
          <el-button type="text" size="small" @click="backToLogin">返回登录</el-button>
        </el-row>
      </el-col>
    </el-card>
  </div>
</template>

<script>
import GetCaptchaButton from "@/components/GetCaptchaButton.vue";
import { validatePhone, validatePassword, validateCaptcha } from "@/utils/validate";
import { phoneCaptchaRegister } from "@/api/auth/user";

export default {
  name: "login",
  components: { GetCaptchaButton },
  data() {
    const validateCheckPassword = (rule, value, callback) => {
      if (value !== this.loginForm.password || value === '') {
        callback(new Error('两次输入密码不一致'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        phone: '',
        captcha: '',
        password: '',
        checkPassword: ''
      },
      loginRules: {
        phone: [{required: true, trigger: 'blur', validator: validatePhone}],
        captcha: [{required: true, trigger: 'blur', validator: validateCaptcha}],
        password: [{required: true, trigger: 'blur', validator: validatePassword}],
        checkPassword: [{required: true, trigger: 'blur', validator: validateCheckPassword}]
      }
    }
  },
  methods: {
    register() {
      this.$refs.loginForm.validate(valid => {
        if(valid) {
          phoneCaptchaRegister(this.loginForm).then(res => {
            if(res.data.code === 200) {
              this.$message.success(res.data.message)
            } else {
              // this.$message.error(res.data.message)
            }
          })
        } else {
          return false
        }
      })
    },
    backToLogin() {
      this.$router.push('/')
    }
  }
}
</script>

<style lang="scss" scoped>
$dark_gray: #bfbfbf;
$white: #ffffff;

.text {
  font-size: 14px;
}

.item {
  padding: 18px 0;
}

.left-title {
  display: block;
  text-align: center;
  margin-top: 5%;
  margin-bottom: 10px;
  font-size: 20px;
}

.QR-img {
  width: 60%;
  height: 60%;
  text-align: center;
  border: solid 1px $dark_gray;
}

.line {
  width: 15px;
  height: 400px;
  border-right: solid $dark_gray 1px;
  margin-top: 10px;
  margin-bottom: 10px;
}

.login-container {
  position: relative;
  background: url("@/assets/image/background.png") no-repeat;
  height: 100%;
  width: 100%;
  overflow: hidden;
  background-size: cover;
  margin: 0;
  padding: 0;
}

.box-card {
  position: absolute;
  width: 750px;
  height: 450px;
  top: 0;
  bottom: 0;
  left: 40%;
  right: 30%;
  margin: auto;
  background-color: $white;
  border-radius: 15px;
}

.login-button {
  margin-top: 30px;
  margin-bottom: 5px;
  width: 100%;
}

.register-box {
  text-align: center;
  margin-left: 20px;
  margin-top: 15px;
  font-size: 20px;
  padding: 0;
}
</style>
